// MARK: - Error
enum SomeError: Error {
    case error0
    case error1
    case error2
}

// MARK: - throw & throws
class A {
    func process(i: Int) throws -> Int {
        guard i != 0 else {
            throw SomeError.error0
        }
        guard i != 1 else {
            throw SomeError.error1
        }
        guard i != 2 else {
            throw SomeError.error2
        }
        
        return 200
    }
}

// MARK: - Catch
let a1 = A()
do {
    let result = try a1.process(i: 1)
    print(result)
} catch SomeError.error0 {
    print(SomeError.error0)
} catch SomeError.error1 {
    print(SomeError.error1)
} catch SomeError.error2 {
    print(SomeError.error2)
}

// MARK: - try?
let result = try? a1.process(i: 2)
//print(result as Any)

func process(a: A, i: Int) throws -> Int {
    return try a.process(i: i)
}
let r = try? process(a: a1, i: 2)
//print(r as Any)

// MARK: - rethrows
// 闭包参数中有throws时，用rethrows抛出
func operate(operation: () throws -> ()) rethrows -> Int{
    try operation()
    
    return 0
}
let operateResult = try? operate {
    throw SomeError.error0
}
//print(operateResult as Any)
